package org.atlantis.datasink.utils;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;

import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

public final class Utils {

	public static final Timer shareTimer = new HashedWheelTimer();

	public static SocketAddress toSocketAddress(String address) {
		boolean isCorrect = address.matches("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\:\\d+$");
		if (!isCorrect) throw new IllegalArgumentException("Invalid socket address: " + address);
		String addrParts[] = address.split(":");
		SocketAddress sa = null;
		sa = new InetSocketAddress(addrParts[0], Integer.parseInt(addrParts[1]));
		return sa;
	}

	public static void assertNull(String attrName, Object attrVal) {
		if (attrVal == null) throw new NullPointerException(attrName + " is null");
	}

	public static void assertEmpty(String attrName, CharSequence attrVal) {
		assertNull(attrName, attrVal);
		if (attrVal.length() == 0) throw new IllegalArgumentException(attrName + " is empty.");
	}

	public static void assertEmpty(String attrName, Collection<?> attrVal) {
		assertNull(attrName, attrVal);
		if (attrVal.isEmpty()) throw new IllegalArgumentException(attrName + " is empty.");
	}

	public static void assertExpr(boolean expr, String desc) {
		if (!expr) throw new IllegalArgumentException(desc);
	}

	private Utils() {
		throw new UnsupportedOperationException();
	}
}
